/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.cell;

import gui.data.ITrackCellData;
import java.awt.Color;
import java.awt.Component;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;

/**
 * A Cell Renderer that represents a track in a JList
 * 
 * @author Pascal
 */
public class TrackCellRenderer extends JPanel implements ListCellRenderer<ITrackCellData> {
    
    private SimpleDateFormat formatter;
    private DecimalFormat numberFormatter;
    
    /**
     * Creates new form TrackCellRenderer
     */
    public TrackCellRenderer() {
        initComponents();
        
        formatter = new SimpleDateFormat("dd. MMM y HH:mm");
        numberFormatter = new DecimalFormat("#0.0");
        
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        labelTrackName = new javax.swing.JLabel();
        labelDistance = new javax.swing.JLabel();
        labelDate = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();

        setBackground(new java.awt.Color(255, 255, 255));

        labelTrackName.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        labelTrackName.setText("Saarrundweg");

        labelDistance.setText("5,0 km");

        labelDate.setText("04. Sep 2013 10:50");

        jSeparator1.setAlignmentY(0.0F);
        jSeparator1.setMinimumSize(new java.awt.Dimension(50, 1));
        jSeparator1.setPreferredSize(new java.awt.Dimension(50, 1));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(labelTrackName)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 80, Short.MAX_VALUE)
                        .addComponent(labelDistance))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(labelDate)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelTrackName)
                    .addComponent(labelDistance))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelDate)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel labelDate;
    private javax.swing.JLabel labelDistance;
    private javax.swing.JLabel labelTrackName;
    // End of variables declaration//GEN-END:variables

    @Override
    public Component getListCellRendererComponent(JList<? extends ITrackCellData> list, ITrackCellData value, int index, boolean isSelected, boolean cellHasFocus) {
        
        labelTrackName.setText(value.getTrackName());
        labelDistance.setText(numberFormatter.format(value.getTrackDistance()) + " km");
        
        Date date = value.getTrackDate().getTime();
        
        labelDate.setText(formatter.format(date));
        
        if (isSelected) {
            setBackground(new Color(171, 203, 255));
        }
        else {
            setBackground(new Color(255, 255, 255));
        }      
        
        return this;
    }
}
